--[[
author  :   jwg
function:   as listview's cell widget
]]

MLListCellView = Class(MLView,'MLListCellView')
MLListCellView.bgImage      = nil;
MLListCellView.focusImage   = nil;
MLListCellView.bgColor 		= nil;
MLListCellView.focusColor   = nil;
MLListCellView.height		= nil;


function MLListCellView:init(width,viewCtrl,cellTemplate,dataTable,id)
	local  cellDataTable = dataTable;--存儲數據的表
	local cellTable = cellTemplate;--存儲模板的表
	self.bgImage = UIImage.imageName(cellTable.bgImg);
	self.focusImage = UIImage.imageName(cellTable.selImg);
	self.bgColor = cellTable.color;
	self.focusColor = cellTable.focusColor;
	self.height = cellTable.height;
	self:initView(width,self.height,viewCtrl,id);

	local  tmpTemplateTable = {};
	self.onPoint = function (evt, x, y)
		local interval =  self.LOSE_FOCUS_INTERVAL;
		if evt == MLView.PointDown then
			self.mDownX = x;
			self.mDownY = y;
			self.mIsFocus= true;
		elseif evt == MLView.PointUp then
			self.mDownX = x;
			self.mDownY = y;
			self.mIsFocus = false;
		elseif evt == MLView.PointMove then
			if math.abs(x-self.mDownX) > interval or math.abs(y-self.mDownY) > interval then
				self.mIsFocus = false;
			end
		end
		self:draw();
		return 1;
	end

	for i,v in ipairs(cellTable.items) do
		if v.xtype == 'button' then
			local buttonView = MLListCellButtonView.new():init(viewCtrl,v,cellDataTable.items[i].text,id);
			self:add(buttonView,v.x,v.y);
		elseif v.xtype == 'img' then
			local  imageView = MLListCellImageView.new():init(viewCtrl,v,UIImage.imageName(cellDataTable.items[i].src),id);
			self:add(imageView,v.x,v.y);
		elseif v.xtype == 'lab' then
			local labelView = MLListCellLabView.new():init(viewCtrl,v,cellDataTable.items[i].text,id);
			self:add(labelView,v.x,v.y);
		elseif v.xtype == 'text' then
			local textView = MLListCellTextView.new():init(viewCtrl,v,cellDataTable.items[i].text,id);
			self:add(textView,v.x,v.y);
		end
	end
	self:draw();
	return self;
end

function MLListCellView:draw()
	local g = self:getGraphic();
    g:clear();
	local image =  self.bgImage;
	local color = self.bgColor;
	if self.mIsFocus then
		image = self.focusImage or self.bgImage;
		color = self.focusColor or self.bgColor;
	end
	if image then		
		g:drawImage(image, 0, 0, self.mWidth, self.mHeight);
	end
	self:setColor(color)
end

function qt_event_point(hashcode, evt, x,y)
	if qtEvent[hashcode]~=nil then
		if qtEvent[hashcode].onPoint ~=nil then
			return qtEvent[hashcode].onPoint(evt,x,y);
			else
				if qtEvent[hashcode].onClick ~=nil then
			 	return true;
			 else
				return false;
			 end
		end
	end
end

--[[
功能：  触摸笔点击事件监听函数
参数：	无	
返回：  无
注解：	层可重写此函数，来监听此事件
]] 
function qt_event_click(hashcode)
	if qtEvent[hashcode]~=nil then
		if qtEvent[hashcode].onClick ~=nil then
		   qtEvent[hashcode].onClick();
		end
	end
end